#include "comm.h"

int main()
{
    int shmid = get_shmid(1024),current_shmid;
    char *buf;
    int index = 1;
	buf = shmat(shmid,NULL, 0 );

    while(1)
    {
		current_shmid=get_shmid(1024);
		if(current_shmid < 0)
			return -1 ;

		if(current_shmid!=shmid)
		{
			shmdt(shmid);
			buf = shmat(current_shmid,NULL, 0 );
			if ( (int)buf == -1 )
			{
				perror("shmat addr error") ;
				return -1 ;
			}
			shmid = current_shmid;
		}
		index = *(buf + 0);
        printf("share memory@0x%x: buf[%d]=%c\n", buf,index, *(buf+index));
        sleep(1);
        if( index == 27)
            break; // 让程序结束
    }
    return 0;
}